package com.eurasia.assignment.infrastructure.hoj;

import org.springframework.stereotype.Component;

import com.eurasia.assignment.domain.hoj.HOJFullCaseJudgement;
import com.eurasia.assignment.service.port.out.HOJFullCaseJudgementPersistencePort;

import lombok.RequiredArgsConstructor;

import java.util.Optional;

/**
 * 完整测试用例判题记录持久化适配器
 */
@Component
@RequiredArgsConstructor
public class HOJFullCaseJudgementPersistenceAdapter implements HOJFullCaseJudgementPersistencePort {

    private final HOJFullCaseJudgementRepository repository;
    private final HOJFullCaseJudgementEntityMapper mapper = HOJFullCaseJudgementEntityMapper.INSTANCE;

    @Override
    public Optional<HOJFullCaseJudgement> findBySubmitId(Long submitId) {
        return repository.findByRunId(submitId).map(mapper::toDomain);
    }

    @Override
    public HOJFullCaseJudgement save(HOJFullCaseJudgement judgement) {
        // 处理ID为0的情况，避免Hibernate将其视为要更新的记录
        HOJFullCaseJudgementEntity entity = mapper.fromDomain(judgement);

        HOJFullCaseJudgementEntity saved = repository.save(entity);
        return mapper.toDomain(saved);
    }
} 